From: Yehuda Katz Date: Tue, 27 May 2014 23:47:04 +0000 (-0700) Subject: Start adding a shell abstraction X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~1046 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/success//%22http:/www.example.com/cgi/success/?a=commitdiff_plain;h=93558906d897c3dd6ed2d8677df74406a53b09e7;p=cargo.git Start adding a shell abstraction --- diff --git a/src/cargo/core/mod.rs b/src/cargo/core/mod.rs index 726f9c4b0..d9bb6d504 100644 --- a/src/cargo/core/mod.rs +++ b/src/cargo/core/mod.rs @@ -36,5 +36,6 @@ pub mod dependency; pub mod manifest; pub mod resolver; pub mod summary; +pub mod shell; mod registry; mod version_req; diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs new file mode 100644 index 000000000..a5d98142b --- /dev/null +++ b/src/cargo/core/shell.rs @@ -0,0 +1,179 @@ +use term; +use term::{Terminal,color}; +use term::color::Color; +use term::attr::Attr; +use std::io::IoResult; +use std::io::stdio::StdWriter; + +pub struct ShellConfig { + color: bool, + verbose: bool +} + +enum AdequateTerminal { + NoColor(BasicTerminal), + Color(Box>) +} + +pub struct Shell { + terminal: AdequateTerminal, + config: ShellConfig +} + +impl Shell { + fn create(out: StdWriter, config: ShellConfig) -> Option { + let term = if out.isatty() { + let term: Option> = Terminal::new(out); + term.map(|t| Color(box t)) + } else { + Some(NoColor(BasicTerminal { writer: out })) + }; + + term.map(|term| Shell { terminal: term, config: config }) + } + + pub fn verbose(&mut self, callback: |&mut Shell| -> IoResult<()>) -> IoResult<()> { + if self.config.verbose { + return callback(self) + } + + Ok(()) + } + + pub fn say(&mut self, message: T, color: Color) -> IoResult<()> { + try!(self.reset()); + try!(self.fg(color)); + try!(self.write_line(message.as_slice())); + try!(self.reset()); + try!(self.flush()); + Ok(()) + } +} + +impl Terminal for Shell { + fn new(out: StdWriter) -> Option { + Shell::create(out, ShellConfig { color: true, verbose: false }) + } + + fn fg(&mut self, color: color::Color) -> IoResult { + match self.terminal { + Color(ref mut c) => c.fg(color), + NoColor(ref mut n) => n.fg(color) + } + } + + fn bg(&mut self, color: color::Color) -> IoResult { + match self.terminal { + Color(ref mut c) => c.bg(color), + NoColor(ref mut n) => n.bg(color) + } + } + + fn attr(&mut self, attr: Attr) -> IoResult { + match self.terminal { + Color(ref mut c) => c.attr(attr), + NoColor(ref mut n) => n.attr(attr) + } + } + + fn supports_attr(&self, attr: Attr) -> bool { + match self.terminal { + Color(ref c) => c.supports_attr(attr), + NoColor(ref n) => n.supports_attr(attr) + } + } + + fn reset(&mut self) -> IoResult<()> { + match self.terminal { + Color(ref mut c) => c.reset(), + NoColor(ref mut n) => n.reset() + } + } + + fn unwrap(self) -> StdWriter { + fail!("Can't unwrap a Shell") + } + + fn get_ref<'a>(&'a self) -> &'a StdWriter { + match self.terminal { + Color(ref c) => c.get_ref(), + NoColor(ref n) => n.get_ref() + } + } + + fn get_mut<'a>(&'a mut self) -> &'a mut StdWriter { + match self.terminal { + Color(ref mut c) => c.get_mut(), + NoColor(ref mut n) => n.get_mut() + } + } +} + +impl Writer for Shell { + fn write(&mut self, buf: &[u8]) -> IoResult<()> { + match self.terminal { + Color(ref mut c) => c.write(buf), + NoColor(ref mut n) => n.write(buf) + } + } + + fn flush(&mut self) -> IoResult<()> { + match self.terminal { + Color(ref mut c) => c.flush(), + NoColor(ref mut n) => n.flush() + } + } +} + +pub struct BasicTerminal { + writer: T +} + +impl Terminal for BasicTerminal { + fn new(out: T) -> Option> { + Some(BasicTerminal { writer: out }) + } + + fn fg(&mut self, _: Color) -> IoResult { + Ok(false) + } + + fn bg(&mut self, _: Color) -> IoResult { + Ok(false) + } + + fn attr(&mut self, _: Attr) -> IoResult { + Ok(false) + } + + fn supports_attr(&self, _: Attr) -> bool { + false + } + + fn reset(&mut self) -> IoResult<()> { + Ok(()) + } + + fn unwrap(self) -> T { + self.writer + } + + fn get_ref<'a>(&'a self) -> &'a T { + &self.writer + } + + fn get_mut<'a>(&'a mut self) -> &'a mut T { + &mut self.writer + } +} + +impl Writer for BasicTerminal { + fn write(&mut self, buf: &[u8]) -> IoResult<()> { + self.writer.write(buf) + } + + fn flush(&mut self) -> IoResult<()> { + self.writer.flush() + } +} + diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 9e6e3d33f..1ee1b8900 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -5,10 +5,11 @@ #![feature(macro_rules,phase)] extern crate collections; +extern crate term; extern crate url; -extern crate hammer; extern crate serialize; extern crate semver; +extern crate hammer; extern crate toml; #[phase(syntax, link)]